package cn.snmp.mutual.uk.stack;

import java.util.*;
import java.io.*;

class AsnSequence extends AsnObject {
    private static final String version_id = "@(#)$Id: AsnSequence.java,v 3.15 2006/11/29 16:35:29 birgit Exp $ Copyright Westhawk Ltd";
    private Vector children;

    AsnSequence() {
        this((byte) 48);
    }

    AsnSequence(final byte oddtype) {
        this.type = oddtype;
        this.children = new Vector(1, 1);
    }

    AsnSequence(final InputStream in, final int len, int pos) throws IOException {
        this();
        if (AsnSequence.debug > 10) {
            System.out.println("AsnSequence(): Length = " + len + ", Pos = " + pos);
        }
        AsnObject a = null;
        while (true) {
            a = this.AsnReadHeader(in, pos);
            if (a == null) {
                break;
            }
            pos += a.headerLength + a.contentsLength;
            this.add(a);
        }
    }

    @Override
    public String toString() {
        return "";
    }

    @Override
    AsnObject add(final AsnObject child) {
        if (child.isCorrect) {
            this.children.addElement(child);
        }
        return child;
    }

    AsnObject replaceChild(final AsnObject oldChild, final AsnObject newChild) {
        AsnObject ret = oldChild;
        final int at = this.children.indexOf(oldChild);
        if (at > -1) {
            this.children.setElementAt(newChild, at);
            ret = newChild;
        }
        return ret;
    }

    @Override
    int size() throws EncodingException {
        final Enumeration childList = this.children.elements();
        int sz = 0;
        while (childList.hasMoreElements()) {
            int cnt = ((AsnObject) childList.nextElement()).size();
            cnt += 1 + this.getLengthBytes(cnt);
            sz += cnt;
        }
        return sz;
    }

    void write(final OutputStream out) throws IOException, EncodingException {
        this.write(out, 0);
    }

    @Override
    void write(final OutputStream out, int pos) throws IOException, EncodingException {
        final int length = this.size();
        this.startPos = pos;
        this.AsnBuildHeader(out, this.type, length);
        if (AsnSequence.debug > 10) {
            System.out.println("\tAsnSequence.write(): begin, startPos = " + this.startPos);
        }
        pos += this.headerLength;
        final Enumeration childList = this.children.elements();
        while (childList.hasMoreElements()) {
            final AsnObject child = (AsnObject) childList.nextElement();
            child.write(out, pos);
            child.startPos = pos;
            pos += child.headerLength + child.contentsLength;
        }
        if (AsnSequence.debug > 10) {
            System.out.println("\tAsnSequence.write(): end");
        }
    }

    @Override
    AsnObject findPdu() {
        AsnObject res = null;
        final Enumeration childList = this.children.elements();
        while (childList.hasMoreElements()) {
            final AsnObject child = (AsnObject) childList.nextElement();
            res = child.findPdu();
            if (res != null) {
                break;
            }
        }
        if (!this.isCorrect && res != null) {
            res.isCorrect = false;
        }
        return res;
    }

    @Override
    AsnObject findTrapPduv1() {
        AsnObject res = null;
        final Enumeration childList = this.children.elements();
        while (childList.hasMoreElements()) {
            final AsnObject child = (AsnObject) childList.nextElement();
            res = child.findTrapPduv1();
            if (res != null) {
                break;
            }
        }
        if (!this.isCorrect && res != null) {
            res.isCorrect = false;
        }
        return res;
    }

    AsnObject getObj(final int offset) {
        AsnObject res = new AsnNull();
        try {
            res = (AsnObject) this.children.elementAt(offset);
        } catch (ArrayIndexOutOfBoundsException ex) {
        }
        return res;
    }

    int getObjCount() {
        return this.children.size();
    }
}
